package controller;

/**
 * Controller class is used to out questions, recomedations and for transfer
 * entered information
 *
 * @version 1 10 jun 2012
 * @author Lyuda
 */
import accumulator.Accumulator;
import advisor.*;
import analyzer.Consultation;

import java.util.ArrayList;
import java.util.List;

import human.Human;
import questionLibrary.*;

public class Controller {

	private Human pokemon;
	private AdvancedAdvisor consultant;
	private QuestionLibrary questionLibrary;
	private QuestionInfo question;
	private Accumulator accumulator;
	private OrganInfo organEffect;
	private InputOutputInterface information;
	int countOfQuestions;

	public Controller(Human pokemon, InputOutputInterface information) {
		this.pokemon = pokemon;
		SqlWorker worker=new SqlWorker();
		questionLibrary = new QuestionLibrary(worker);
		accumulator = new Accumulator();
		consultant = new AdvancedAdvisor(this.pokemon);
		consultant.addAdvisors(new BrainAdvisor(this.pokemon));
		consultant.addAdvisors(new LiverAdvisor(this.pokemon));
		this.information = information;
		countOfQuestions=questionLibrary.getCountOfQuestions();
	}

	public Controller(Human pokemon) {
		this(pokemon, new HandlerInputOutput());
	}

	public void outputQuestions() {
		int[] answers;
		for (int numberOfQuestion = 0; numberOfQuestion < questionLibrary
				.getCountOfQuestions(); numberOfQuestion++) {
			answers = null;
			question = questionLibrary.getQuestion(numberOfQuestion);
			information.print(question.toString());
			while (answers == null) {
				try {
					answers = information.readAnswer();
				} catch (NumberFormatException ex) {
					information
							.print("В вашем ответете должны быть только цыфры!\nПовторите ответ.");
				}
			}
			organEffect = countEffect(answers);
			accumulator.addInfo(organEffect);
		}
		accumulator.pokemonInfluence(pokemon);
	}

	public void getRecomedation() {
		try {
			consultant.work();
			List<Consultation> consultations = consultant.getConsultations();
			for (Consultation consultation : consultations) {
				information.print(consultation.toString());
			}
		} catch (Exception ex) {
			information.print(ex.getMessage());
		}
	}

	private OrganInfo countEffect(int[] answers) {
		OrganInfo result = new OrganInfo();
		for (int numberOfOrgan = 0; numberOfOrgan < SomeConstants.COUNT_OF_ORGANS; numberOfOrgan++) {
			for (int numberOfParam = 0; numberOfParam < SomeConstants.COUNT_OF_PARAMS; numberOfParam++) {
				result.setParameter(numberOfOrgan, numberOfParam, question
						.getEffect(numberOfOrgan, numberOfParam, answers));
			}
		}
		return result;
	}

	public String getQuestion(int questionNumber) {
		question = questionLibrary.getQuestion(questionNumber);
		return question.getQuestion();
	}

	public String[] getAnswers() {
		return question.getAnswers();
	}

	public void setAnswers(int... answers) {
		organEffect = countEffect(answers);
		accumulator.addInfo(organEffect);
	}

	public void stopAsking() {
		accumulator.pokemonInfluence(pokemon);
	}

	public String[] getRecomedationArray() {
		List<String> sb = new ArrayList<String>();
		try {
			consultant.work();
			List<Consultation> consultations = consultant.getConsultations();

			for (Consultation consultation : consultations) {
				sb.add(consultation.toString());
			}

		} catch (Exception ex) {
			sb.add(ex.getMessage());
		}
		return sb.toArray(new String[sb.size()]);
	}

	public int getCountOfQuestions() {		
		return questionLibrary.getCountOfQuestions();
	}
}